{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arch.unitroot import ADF\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>close</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-12-01</th>\n",
       "      <td>2015-12-01</td>\n",
       "      <td>3562.319</td>\n",
       "      <td>3617.305</td>\n",
       "      <td>3591.696</td>\n",
       "      <td>3536.116</td>\n",
       "      <td>13653656000</td>\n",
       "      <td>176598810624</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-02</th>\n",
       "      <td>2015-12-02</td>\n",
       "      <td>3587.758</td>\n",
       "      <td>3725.854</td>\n",
       "      <td>3721.955</td>\n",
       "      <td>3573.403</td>\n",
       "      <td>21353886400</td>\n",
       "      <td>260938694656</td>\n",
       "      <td>0.036267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-03</th>\n",
       "      <td>2015-12-03</td>\n",
       "      <td>3709.547</td>\n",
       "      <td>3758.446</td>\n",
       "      <td>3749.299</td>\n",
       "      <td>3693.137</td>\n",
       "      <td>19776212800</td>\n",
       "      <td>236698992640</td>\n",
       "      <td>0.007347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-04</th>\n",
       "      <td>2015-12-04</td>\n",
       "      <td>3719.734</td>\n",
       "      <td>3726.124</td>\n",
       "      <td>3677.592</td>\n",
       "      <td>3667.291</td>\n",
       "      <td>15119952000</td>\n",
       "      <td>196455596032</td>\n",
       "      <td>-0.019125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-07</th>\n",
       "      <td>2015-12-07</td>\n",
       "      <td>3681.582</td>\n",
       "      <td>3699.337</td>\n",
       "      <td>3687.607</td>\n",
       "      <td>3658.578</td>\n",
       "      <td>11388315200</td>\n",
       "      <td>161220067328</td>\n",
       "      <td>0.002723</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  date      open      high     close       low       volume  \\\n",
       "date                                                                          \n",
       "2015-12-01  2015-12-01  3562.319  3617.305  3591.696  3536.116  13653656000   \n",
       "2015-12-02  2015-12-02  3587.758  3725.854  3721.955  3573.403  21353886400   \n",
       "2015-12-03  2015-12-03  3709.547  3758.446  3749.299  3693.137  19776212800   \n",
       "2015-12-04  2015-12-04  3719.734  3726.124  3677.592  3667.291  15119952000   \n",
       "2015-12-07  2015-12-07  3681.582  3699.337  3687.607  3658.578  11388315200   \n",
       "\n",
       "                  amount    return  \n",
       "date                                \n",
       "2015-12-01  176598810624       NaN  \n",
       "2015-12-02  260938694656  0.036267  \n",
       "2015-12-03  236698992640  0.007347  \n",
       "2015-12-04  196455596032 -0.019125  \n",
       "2015-12-07  161220067328  0.002723  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('../data/000300_index.csv')\n",
    "df.index = df['date']\n",
    "df.sort_index(inplace=True)\n",
    "df['return'] = df['close']/df['close'].shift(1) - 1\n",
    "ret = df['return'].dropna() #这行很重要，需要把NaN的值去掉\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>Augmented Dickey-Fuller Results</caption>\n",
       "<tr>\n",
       "  <td>Test Statistic</td>    <td>-6.351</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>P-value</td>            <td>0.000</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>Lags</td>                  <td>14</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'arch.unitroot.unitroot.ADF'>\n",
       "\"\"\"\n",
       "   Augmented Dickey-Fuller Results   \n",
       "=====================================\n",
       "Test Statistic                 -6.351\n",
       "P-value                         0.000\n",
       "Lags                               14\n",
       "-------------------------------------\n",
       "\n",
       "Trend: Constant\n",
       "Critical Values: -3.44 (1%), -2.87 (5%), -2.57 (10%)\n",
       "Null Hypothesis: The process contains a unit root.\n",
       "Alternative Hypothesis: The process is weakly stationary.\n",
       "\"\"\""
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf = ADF(ret)\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'   Augmented Dickey-Fuller Results   \\n=====================================\\nTest Statistic                 -6.351\\nP-value                         0.000\\nLags                               14\\n-------------------------------------\\n\\nTrend: Constant\\nCritical Values: -3.44 (1%), -2.87 (5%), -2.57 (10%)\\nNull Hypothesis: The process contains a unit root.\\nAlternative Hypothesis: The process is weakly stationary.'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf.summary().as_text()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>Augmented Dickey-Fuller Results</caption>\n",
       "<tr>\n",
       "  <td>Test Statistic</td>    <td>-1.477</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>P-value</td>            <td>0.545</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <td>Lags</td>                  <td>15</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'arch.unitroot.unitroot.ADF'>\n",
       "\"\"\"\n",
       "   Augmented Dickey-Fuller Results   \n",
       "=====================================\n",
       "Test Statistic                 -1.477\n",
       "P-value                         0.545\n",
       "Lags                               15\n",
       "-------------------------------------\n",
       "\n",
       "Trend: Constant\n",
       "Critical Values: -3.44 (1%), -2.87 (5%), -2.57 (10%)\n",
       "Null Hypothesis: The process contains a unit root.\n",
       "Alternative Hypothesis: The process is weakly stationary.\n",
       "\"\"\""
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf = ADF(df['close'])\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'   Augmented Dickey-Fuller Results   \\n=====================================\\nTest Statistic                 -1.477\\nP-value                         0.545\\nLags                               15\\n-------------------------------------\\n\\nTrend: Constant\\nCritical Values: -3.44 (1%), -2.87 (5%), -2.57 (10%)\\nNull Hypothesis: The process contains a unit root.\\nAlternative Hypothesis: The process is weakly stationary.'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf.summary().as_text()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 3.85709512,  5.9335164 ,  6.39428791,  6.49998591, 10.06279834,\n",
       "        10.50812978, 11.42299801, 11.4317141 , 18.23071851, 18.36597625,\n",
       "        20.5095434 , 29.80901729]),\n",
       " array([0.04953602, 0.0514699 , 0.09392607, 0.16479127, 0.07347531,\n",
       "        0.10482076, 0.12120279, 0.17841658, 0.03258921, 0.04909513,\n",
       "        0.03882325, 0.00298328]))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from statsmodels.tsa import stattools\n",
    "lb = stattools.q_stat(stattools.acf(ret)[1:13],len(ret))\n",
    "lb #是白噪声？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0029832845561380407"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lb[1][-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lb2 = stattools.q_stat(stattools.acf(df['close'])[1:13],len(ret))\n",
    "lb2[1][-1] #p值很小，不是白噪声"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
